# -*- encoding: utf-8 -*-
"""

PyCharm 创建一个算数的验证码操作界面
2024年10月25日12时06分
by wenyang

文件主要内容:


"""
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random


def main():
    # 创建一个随机的数字
    def random_num():
        return random.randint(0,100)
    # 创建一个随机的算符符号
    def random_operator():
        return random.choice(("+", "-", "*", "/"))
    print(str(random_operator()))
    # 对背景创建一个随机的颜色
    def random_color():
        return random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)
    # 对字体创建一个随机的颜色
    def random_color2():
        return random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)
    # 设置字体的格式
    font = ImageFont.truetype("C:/Windows/Fonts/Arial.ttf", 30)
    font_operator = ImageFont.truetype("C:/Windows/Fonts/Arial.ttf", 40)
    # 开始创建一个图片
    # 设置图片的宽度和高度
    width = 60 * 4
    height = 60
    image = Image.new("RGB", (width, height), (255, 255, 255))
    # 创建一个画笔 就是draw对象 来绘制图像
    draw = ImageDraw.Draw(image)
    # 填充背景色
    for x in range(width):
        for y in range(height):
            draw.point((x, y), fill = random_color())
    # 画干扰线 就是可以通过使用两点确定一条直线的思路来进行创建
    # 比如画四条干扰线
    for i in range(4):
        x1 = random.randint(0, width)
        x2 = random.randint(0, width)
        y1 = random.randint(0, height)
        y2 = random.randint(0, height)
        draw.line((x1, y1, x2, y2), fill = random_color())
    # 画验证码
    draw.text((60, 10), str(random_num()), font = font, fill = random_color2())
    draw.text((100, 10), str(random_operator()), font = font_operator, fill = random_color2())
    draw.text((120, 10), str(random_num()), font = font, fill = random_color2())
    draw.text((150, 10), "=", font = font, fill = random_color2())
    # 对图像进行 模糊处理
    image = image.filter(ImageFilter.BLUR)
    # 展示图片
    image.show()



if __name__ == "__main__":
    main()
